package org.mclll.xwangplugin.town.command.executor;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import org.bukkit.entity.Player;
import org.mclll.xwangplugin.town.database.domain.TownApplyDO;
import org.mclll.xwangplugin.town.database.domain.TownDO;
import org.mclll.xwangplugin.town.database.domain.TownPlayerDO;
import org.mclll.xwangplugin.town.database.mapper.manage.TownApplyManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownManage;
import org.mclll.xwangplugin.town.database.mapper.manage.TownPlayerManage;
import org.mclll.xwangplugin.town.enums.ApplyStatus;
import org.mclll.xwangplugin.town.enums.ApplyType;
import org.mclll.xwangplugin.town.enums.PlayerTownIdentity;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.mclll.xwangplugin.town.config.TownContext.*;

/**
 * @author: geduo
 * @date: 2023/9/23 19:35
 */
public class InvitePlayerExecutor {

    public static void invitePlayer(Player player, String[] args) {
        UUID uniqueId = player.getUniqueId();
        TownPlayerDO playerParam = new TownPlayerDO();
        playerParam.setUuid(uniqueId.toString());
        List<TownPlayerDO> townPlayerList = TownPlayerManage.selectList(playerParam);
        // 只有镇长才有权限指令这个命令
        if (townPlayerList.isEmpty() ||
                townPlayerList.get(0).getIdentity() == PlayerTownIdentity.GENSHIN ||
                townPlayerList.get(0).getIdentity() == PlayerTownIdentity.MEMBER) {
            player.sendMessage(NOT_PERMISSION);
            return;
        }
        // poitown invite <玩家名>
        if (args.length < 2) {
            player.sendMessage(INVITE_PLAYER_COMMAND_FORMAT);
            return;
        }
        if (args.length != 2) {
            return;
        }
        String inviteTownUuid = townPlayerList.get(0).getTownUuid();
        TownDO townParam = new TownDO();
        townParam.setUuid(inviteTownUuid);
        TownDO town = TownManage.selectList(townParam).get(0);
        String playerName = args[1].trim();
        TownPlayerDO townPlayerParam = new TownPlayerDO();
        townPlayerParam.setName(playerName);
        List<TownPlayerDO> invitePlayerList = TownPlayerManage.selectList(townPlayerParam);
        Player invitedPlayer = player.getServer().getPlayer(playerName);
        boolean success = false;
        Integer applyId = null;
        if (!invitePlayerList.isEmpty()) {
            TownPlayerDO invitePlayer = invitePlayerList.get(0);
            if (invitePlayer.getIdentity() != PlayerTownIdentity.GENSHIN) {
                player.sendMessage(String.format(PLAYER_ALREADY_HAVE_TOWN, playerName));
                return;
            }
            // 创建邀请记录
            applyId = createApplyData(invitePlayer.getUuid(), town);
            success = true;
        } else {
            player.sendMessage(PLAYER_NOT_FOUND);
        }
        if (success) {
            player.sendMessage(String.format(INVITE_PLAYER_SUCCESS, playerName));
            if (invitedPlayer != null && invitedPlayer.isOnline()) {
                TownApplyDO townApplyDO = new TownApplyDO();
                townApplyDO.setTownName(town.getName());
                townApplyDO.setId(applyId);
                sendInviteMessage(invitedPlayer, Collections.singletonList(townApplyDO));
            }
        }
    }

    public static void sendInviteMessage(Player invitedPlayer, List<TownApplyDO> applyList) {
        if (applyList == null) {
            TownApplyDO selectParam = new TownApplyDO();
            selectParam.setPlayerUuid(invitedPlayer.getUniqueId().toString());
            selectParam.setStatus(ApplyStatus.APPLYING);
            applyList = TownApplyManage.selectList(selectParam);
        }
        TextComponent.Builder message = Component.text();
        for (TownApplyDO town : applyList) {
            message.append(Component.text("    "))
                    .append(Component.text(String.format(INVITE_PLAYER_SUCCESS_SEND_PLAYER, town.getTownName())))
                    .append(Component.text(CONFIRM_BUTTON)
                            .clickEvent(ClickEvent.runCommand("/poitown accept " + town.getId()))
                            .hoverEvent(HoverEvent.showText(Component.text(CONFIRM_BUTTON)))
                    )
                    .append(Component.text("    "))
                    .append(Component.text(CANCEL_BUTTON)
                            .clickEvent(ClickEvent.runCommand("/poitown reject " + town.getId()))
                            .hoverEvent(HoverEvent.showText(Component.text(CANCEL_BUTTON)))
                    );
        }
        invitedPlayer.sendMessage(message.build());
    }

    private static Integer createApplyData(String playerUuid, TownDO town) {
        TownApplyDO apply = new TownApplyDO();
        apply.setTownUuid(town.getUuid());
        apply.setPlayerUuid(playerUuid);
        apply.setStatus(ApplyStatus.APPLYING);
        apply.setTownName(town.getName());
        apply.setTownParty(town.getTownParty());
        apply.setType(ApplyType.LEADER_INVITATION);
        TownApplyManage.insert(apply);
        return apply.getId();
    }
}
